package org.bonorsoft.modules.system.exception;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.bonorsoft.modules.control.entity.*;
import org.bonorsoft.modules.control.service.*;
import org.bonorsoft.modules.system.entity.SysUser;
import org.bonorsoft.modules.system.service.ISysUserService;
import org.bonorsoft.modules.wechat.entity.WxRequest;
import org.bonorsoft.modules.wechat.service.IWxRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.UUID;

@Controller
public class DetailError {
    @Autowired
    private IAddressService addressService;
    @Autowired
    private IHouseService houseService;
    @Autowired
    private IUnitService unitService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IHouseDisplayService houseDisplayService;
    @Autowired
    private IUnitDisplayService unitDisplayService;
    @Autowired
    private IWxRequestService wxRequestService;

    private String prefix = "errorDetail";

    @RequestMapping("/detailError/{dzbm}")
    public String detailError(@PathVariable("dzbm") String dzbm, ModelMap map) {
        Address address = addressService.getById(dzbm);
        //随手拍内容，显示3条以内
        List<WxRequest> request = wxRequestService.list();
        if (request.size() <= 3) {
            map.put("request", request);
        } else {
            List<WxRequest> req = request.subList(0, 3);
            map.put("request", req);
        }
        //责任民警、网格员
        if (address.getPoliceId() != null) {
            SysUser police = userService.getById(address.getPoliceId());
            map.put("police", police);//责任民警
        }
        if (address.getGridMemberId() != null) {
            SysUser gridMember = userService.getById(address.getGridMemberId());
            map.put("gridMember", gridMember);//网格员
        }
        //判断该地址是房屋还是单位
        int house_count = houseService.count(new QueryWrapper<House>().eq("dzbm", dzbm));
        int unit_count = unitService.count(new QueryWrapper<Unit>().eq("dwdz_dzbm", dzbm));
        //房屋
        if (house_count > 0) {
            House house = houseService.getOne(new QueryWrapper<House>().eq("dzbm", dzbm));
            map.put("address", address);//标准地址
            int houseDisplayCount = houseDisplayService.count(new QueryWrapper<HouseDisplay>().eq("dzbm", dzbm));
            if (houseDisplayCount > 0) {
                map.put("house", house);//房屋信息
                HouseDisplay houseDisplay = houseDisplayService.getOne(new QueryWrapper<HouseDisplay>().eq("dzbm", dzbm));
                map.put("houseDisplay", houseDisplay);
                return prefix + "/houseError";
            } else {
                HouseDisplay houseDisplay = new HouseDisplay();
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                houseDisplay.setHouseDisplayId(uuid);
                houseDisplay.setDzbm(dzbm);
                houseDisplayService.save(houseDisplay);
                map.put("houseDisplay", houseDisplay);
                return prefix + "/houseError";
            }
        }
        //单位
        else if (unit_count > 0) {
            Unit unit = unitService.getOne(new QueryWrapper<Unit>().eq("dwdz_dzbm", dzbm));
            map.put("address", address);//标准地址
            int unitDisplayCount = unitDisplayService.count(new QueryWrapper<UnitDisplay>().eq("dwdz_dzbm", dzbm));
            if (unitDisplayCount > 0) {
                map.put("unit", unit);//单位信息
                UnitDisplay unitDisplay = unitDisplayService.getOne(new QueryWrapper<UnitDisplay>().eq("dwdz_dzbm", dzbm));
                map.put("unitDisplay", unitDisplay);
                return prefix + "/unitError";
            } else {
                UnitDisplay unitDisplay = new UnitDisplay();
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                unitDisplay.setUnitDisplayId(uuid);
                unitDisplay.setDwdzDzbm(dzbm);
                unitDisplayService.save(unitDisplay);
                map.put("unitDisplay", unitDisplay);
                return prefix + "/unitError";
            }
        }
        //未采集
        else {
            map.put("address", address);//标准地址
            return prefix + "/error";
        }
    }

    //公安服务 更多
    @RequestMapping("/gafwMore")
    public String gafwMore() {
        return prefix + "/gafwMore";
    }

    //便民服务 更多
    @RequestMapping("/bmfwMore")
    public String bmfwMore() {
        return prefix + "/bmfwMore";
    }
}

